package com.example;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;

/**
 * Created with IntelliJ IDEA.
 * User: Jasic
 * Date: 12-11-7
 */
public class SmsBroadcastReceiver extends BroadcastReceiver{

    public SmsBroadcastReceiver(){

        System.out.println("SmsBroadcastReceiver's construture..");
    }

    @Override
    public void onReceive(Context context, Intent intent) {

        System.out.println("Receive message...");

        /**
         * 处理短信
         */
        Bundle bundle = intent.getExtras();
        Object[] myObjects = (Object[]) bundle.get("pdus"); // 获得短信

        SmsMessage[] smsMessage = new SmsMessage[myObjects.length];
        System.out.println("smsMessage's length is " + smsMessage.length);
        for (int i = 0; i < smsMessage.length; i++) {
            smsMessage[i] = SmsMessage.createFromPdu((byte[]) myObjects[i]);
            System.out.println("SmsMessage content is [" + smsMessage[i].getDisplayMessageBody() +"]");
        }

    }
}
